package com.example.smartcanteen.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.smartcanteen.mapper.Order1Mapper;
import com.example.smartcanteen.model.*;
import com.example.smartcanteen.utils.ResponseMessage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Order1Service {
    @Autowired
    private Order1Mapper order1Mapper;


    //查看订单
    public ResponseMessage select(int studentID) {
        ResponseMessage responseMessage = new ResponseMessage();

        //根据学生ID查看指定学生的下单记录
        MPJLambdaWrapper<Order1> orderDTOMPJLambdaWrapper = new MPJLambdaWrapper<>();
        orderDTOMPJLambdaWrapper.selectAll(Order1.class)
                .select("t1.goodsname,t1.url,t1.score")
                .leftJoin(Goods.class, Goods::getId, Order1::getGoodsID)
                .eq("studentID", studentID);
        List<OrderDTO> orderDTOS = order1Mapper.selectJoinList(OrderDTO.class, orderDTOMPJLambdaWrapper);
        responseMessage.setData(orderDTOS);
//        QueryWrapper<Order1> order1QueryWrapper = new QueryWrapper<>();
//        order1QueryWrapper.eq("studentID", studentID);
//        responseMessage.setData(order1Mapper.selectList(order1QueryWrapper));
        return responseMessage;
    }


    public ResponseMessage selectByShop(int shopID) {
        ResponseMessage responseMessage = new ResponseMessage();

        //根据学生ID查看指定学生的下单记录
        MPJLambdaWrapper<Order1> orderDTOMPJLambdaWrapper = new MPJLambdaWrapper<>();
        orderDTOMPJLambdaWrapper.selectAll(Order1.class)
                .select("t1.goodsname,t1.url,t1.score")
                .leftJoin(Goods.class, Goods::getId, Order1::getGoodsID)
                .eq("t.shopID", shopID);
        List<OrderDTO> orderDTOS = order1Mapper.selectJoinList(OrderDTO.class, orderDTOMPJLambdaWrapper);
        responseMessage.setData(orderDTOS);
//        QueryWrapper<Order1> order1QueryWrapper = new QueryWrapper<>();
//        order1QueryWrapper.eq("studentID", studentID);
//        responseMessage.setData(order1Mapper.selectList(order1QueryWrapper));
        return responseMessage;
    }


    //商家修改订单状态
    public ResponseMessage changeOrderStateByShop(int id) {
        ResponseMessage responseMessage = new ResponseMessage();
        UpdateWrapper<Order1> orderUpdateWrapper = new UpdateWrapper<>();
        orderUpdateWrapper.eq("id", id)
                .set("state", "商家已完成");
        Boolean success = order1Mapper.update(orderUpdateWrapper) != 0;
        responseMessage.setSuccess(success);
        return responseMessage;
    }


    //学生修改订单状态
    public ResponseMessage changeOrderStateByStudent(int id) {
        ResponseMessage responseMessage = new ResponseMessage();
        UpdateWrapper<Order1> orderUpdateWrapper = new UpdateWrapper<>();
        orderUpdateWrapper.eq("id", id)
                .set("state", "订单已完成");
        Boolean success = order1Mapper.update(orderUpdateWrapper) != 0;
        responseMessage.setSuccess(success);
        return responseMessage;
    }
}
//    生成订单
//    public ResponseMessage create(Order1 order1) {
//        ResponseMessage responseMessage = new ResponseMessage();
//        responseMessage.setSuccess(order1Mapper.insert(order1) != 0);
//        return responseMessage;
//    }
//}
